#!/usr/bin/env ruby

# Copyright (C) 2006-2008 by Sergio Pistone
# sergio_pistone@yahoo.com.ar
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

require File.expand_path( File.dirname( __FILE__ ) + "/../i18n/i18n" )
require File.expand_path( File.dirname( __FILE__ ) + "/../utils/strings" )
require File.expand_path( File.dirname( __FILE__ ) + "/../utils/logger" )
require File.expand_path( File.dirname( __FILE__ ) + "/optionsparser" )
require File.expand_path( File.dirname( __FILE__ ) + "/plugins" )

class Application

	def initialize( featuring_fix, cleanup_lyrics, submit, review=true,
					prompt_autogen=false, prompt_no_lyrics=false, username=nil, password=nil )
		@featuring_fix = featuring_fix
		@submit_plugin = nil
		@logger = Logger.new( "#{ENV["HOME"]}/.wikilyrics.log" )
		Plugins.all_plugins().each() do |plugin|
			plugin.cleanup_lyrics = cleanup_lyrics
			plugin.logger = @logger
			if plugin.plugin_name() == submit
				@submit_plugin = plugin
				@submit_plugin.set_submit_settings( username, password, review, prompt_autogen, prompt_no_lyrics )
			end
		end
	end

	def finalize()
		@logger.finalize() if @logger
	end

	def Application.notify( message )
		puts "Wiki-Lyrics: " + message.gsub( /\/?<[^>]+>/, "" )
	end

	def notify( message )
		self.class.notify( message )
	end

	def restore_session( session_file )
		@submit_plugin.restore_session( session_file ) if @submit_plugin
	end

	def save_session( session_file )
		@submit_plugin.save_session( session_file ) if @submit_plugin
	end

	def fetch_lyrics( request )

		used_plugins = Plugins.all_plugins()

		response = nil
		response_plugin = nil
		shown_review_dialog = false
		submit_plugin_searched = false

		used_plugins.each() do |plugin|
			begin
				submit_plugin_searched = true if plugin == @submit_plugin
				response = plugin.lyrics_full_search( request )
				if response.lyrics
					response_plugin = plugin
					break
				end
			rescue TimeoutError
				notify( I18n.get( "cli.application.plugintimeout", plugin.plugin_name(), plugin.site_host() ) )
			end
		end

 		if @submit_plugin
			shown_review_dialog = @submit_plugin.wiki_process_response( response, response_plugin, submit_plugin_searched )
		end

		return response, response_plugin, shown_review_dialog

	end

	def process( artist, title, album, year )

		if @featuring_fix
			artist = Strings.cleanup_artist( artist, title )
			title  = Strings.cleanup_title( title )
		end

		notify( I18n.get( "cli.application.searchinglyrics", title, artist ) )

		request = Lyrics::Request.new( artist, title, album, year )
		response, response_plugin, shown_review_dialog = fetch_lyrics( request )

		if response.lyrics
			artist = response.artist ? response.artist : response.request.artist
			title = response.title ? response.title : response.request.title
			notify( I18n.get( "cli.application.lyricsfound", title, artist, response_plugin.site_name ) )
			if ! shown_review_dialog
				if GUI.get_current_toolkit()
					GUI.show_lyrics_dialog( {
						"artist" => artist,
						"title" => title,
						"lyrics" => response.lyrics,
						"site_name" => response_plugin.site_name
					} )
				else
					puts "\n#{response.lyrics}\n\n"
				end
			end
		else
			notify( I18n.get( "cli.application.nolyricsfound", request.title, request.artist ) )
		end

	end

end

if $0 == __FILE__

	options = OptionsParser.parse( ARGV )

	Plugins.load_plugins( options.sites )

	HTTP.set_proxy_settings( options.proxy ) if options.proxy
	GUI.set_toolkit_priority( options.toolkits ) if options.toolkits.size > 0

	app = Application.new(
		options.feat_fix,
		options.cleanup,
		options.submit,
		options.review,
		options.prompt_autogen,
		options.prompt_no_lyrics,
		options.username,
		options.password
	)

	begin

		app.restore_session( options.session_file ) if options.session_file

		if options.batch_file
			app.notify( I18n.get( "cli.application.batchmode" ) )
			file = File.new( options.batch_file, "r" )
			count = 0
			while ( line = file.gets )
				params = line.split( ";" )
				next if params.length < 2
				app.process( params[0], params[1], params[2], params[3] )
				count += 1
			end
			notify( I18n.get( "cli.application.processedcount", count ) )
			file.close()
		else
			app.process( options.artist, options.title, options.album, options.year )
		end

		app.save_session( options.session_file ) if options.session_file

	ensure

		app.finalize()

	end

end
